package com.julu.o2o.util;
import com.alibaba.fastjson.JSON;

/**
 * 数组工具类
 * @author yangtonggan
 * @date 2016-4-6
 */
public class ArrayUtil {
	/**
	 * 把以逗号风格的字符串转为json数组格式字符串
	 * @param str
	 * @return
	 */
	public static String formatJsonStr(String str){
		if(str == null || str.length() == 0){
			return "[]";
		}
		StringBuilder sb = new StringBuilder();
		sb.append("[");
		String[] array = str.split(",");
		int size = array.length;
		for(int i = 0 ; i < size ; i++){			
			sb.append("\""+array[i]+"\"");
			sb.append((i==size-1)?"":",");
		}			
		sb.append("]");
		return sb.toString();
	}
	
	public static String jsonFormat(String str){
		if(str == null || str.length() == 0){
			return "[]";
		}
		String[] data = str.split(",");
		String jsonStr = JSON.toJSONString(data);  
		return jsonStr;
	}
	
	/**
	 * 字符串数组转字符串
	 * @param array
	 * @param split
	 * @return
	 */
	public static String join(String[] array,String join){
		StringBuilder sb = new StringBuilder();
		if(isEmpty(array)){
			return "";
		}
		int size = array.length;		
		for(int i = 0 ; i < size ; i++){
			sb.append(array[i]);			
			sb.append((i==size-1)?"":join);
		}		
		return sb.toString();
	}
	
	 /**
	  * 把以逗号分割的字符串转为数组
	  */
	public static String[] toArray(String str){
		String[] data = str.split(",");
		return data;
	}
	/**
	 * 判断数组是否为空
	 * @param strs
	 * @return
	 */
	public static boolean isEmpty(String[] strs){
		if(strs == null || strs.length == 0){
			return true;
		}else{
			return false;
		}
	}
	public static boolean isEmpty(Object[] strs){
		if(strs == null || strs.length == 0){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 判断字符串是否不为空
	 */
	public static boolean notEmpty(String[] strs){
		return !isEmpty(strs);
	}
	
	/**
	 * 判断对象是否不为空
	 */
	public static boolean notEmpty(Object[] strs){
		return !isEmpty(strs);
	}
	
}
